package com.shd.edanpei.presenter;

import android.os.Bundle;

import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.dialog.DialogUtil;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.model.NoDataResponse;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.ui.activity.PhoneReset2Activity;
import com.shd.edanpei.ui.activity.PhoneResetActivity;
import com.shd.edanpei.util.ToastUtil;
import com.shd.edanpei.util.Validator;
import com.shd.edanpei.db.DaoManager;
import com.shd.edanpei.enums.SMSTypeEnum;
import com.shd.edanpei.presenter.contract.PhoneResetContract;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2018/4/4.
 * yang-wen-wen
 */

public class PhoneResetPresenter extends BasePresenterImpl<PhoneResetContract.PhoneResetIView>
        implements PhoneResetContract.PhoneResetIPresenter {

    public PhoneResetPresenter(PhoneResetContract.PhoneResetIView mView) {
        super(mView);
    }

    @Override
    public void getCode(final String phone) {
        if (!Validator.isMobile(phone)) {
            ToastUtil.showToast("请输入正确手机号");
            return;
        }
        if (phone.equals(DaoManager.getInstance().getUserBean().getMobile())) {
            ToastUtil.showToast("新手机号与当前账号一致");
            return;
        }

        DialogUtil.infoDialog(mBaseIView.getContext(), "确认手机号码", "我们将发送验证码短信到这个号码：" + phone, null, null,
                new DialogUtil.SureListener() {
                    @Override
                    public void sure() {
                        mBaseIView.showLoading("");
                        ApiCreator.getApi().sendSms(phone, SMSTypeEnum.updateMobile.getValue(),"","")
                                .compose(RxTransformer.<NoDataResponse>errorHandle())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new BaseObserver<NoDataResponse>() {
                                    @Override
                                    protected void onSuccess(NoDataResponse data) {
                                        ToastUtil.showToast("短信发送成功");
                                        Bundle bundle = new Bundle();
                                        bundle.putString(Constant.PHONE, phone);
                                        mBaseIView.startActivity(PhoneReset2Activity.class, bundle);
                                        mBaseIView.getContext().finish();

                                    }

                                    @Override
                                    public void onFinish() {
                                        mBaseIView.stopLoading();
                                    }
                                });
                    }
                });

    }

    @Override
    public void getCode2(final String phone) {
        if (!Validator.isMobile(phone)) {
            ToastUtil.showToast("请输入正确手机号");
            return;
        }
        ApiCreator.getApi().sendSms(phone, SMSTypeEnum.updateMobile.getValue(),"","")
                .compose(RxTransformer.<NoDataResponse>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<NoDataResponse>() {
                    @Override
                    protected void onSuccess(NoDataResponse data) {
                        ToastUtil.showToast("短信发送成功");
                    }
                });
    }

    @Override
    public void submit(final String phone, final String code) {
        if (!Validator.isMobile(phone)) {
            ToastUtil.showToast("请输入正确手机号");
            return;
        }
        mBaseIView.showLoading("");
        ApiCreator.getApi().updataMobile(phone, code)
                .compose(RxTransformer.<NoDataResponse>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<NoDataResponse>() {
                    @Override
                    protected void onSuccess(NoDataResponse data) {
                        DialogUtil.showSuccessDialog(mBaseIView.getContext(), new DialogUtil.SureListener() {
                            @Override
                            public void sure() {
                                DaoManager.getInstance().getUserBean().setMobile(phone);
                                PhoneResetActivity.instance.finish();
                                mBaseIView.getContext().finish();
                            }
                        }, "更改成功！", "新手机号：" + phone);
                    }

                    @Override
                    public void onFinish() {
                        mBaseIView.stopLoading();
                    }
                });
    }
}
